package fr.bordeaux1.planingdesetudiants.controler;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import fr.bordeaux1.planingdesetudiants.controler.db.IDBPlaningDesEtudiants;
import fr.bordeaux1.planingdesetudiants.model.Etudiant;
import fr.bordeaux1.planingdesetudiants.model.Ue;
/**
 * 
 * @author anroy
 *
 */
public class ParserXML extends DefaultHandler {
	
	//base ou sont enregitrés les éléments lus
	private IDBPlaningDesEtudiants db;
	
	//UE en cours
	private Ue currentUe;
	
	//les étudiants de l'UE en cours
	private List<Etudiant> currentEtudiants;
	private Etudiant currentEtudiant;
	
	//pour la lecture des balise texte
	private StringBuffer currentBuffer;
	
	
	/**
	 * Point d'entr� pour parser un fichier des �tudiants  
	 * @param db
	 * @param xmlfile
	 */
	public void parseXmlEtudiants(IDBPlaningDesEtudiants db, File xmlfile) throws ParserConfigurationException, SAXException, IOException{
		this.db = db;
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		parser.parse(xmlfile, this);
	}
	
	
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		if(currentBuffer!=null)
			currentBuffer.append(new String(ch, start, length));
	}

	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if(qName.equals("G_ELP")){
			currentUe = new Ue();
			currentEtudiants = new ArrayList<Etudiant>();
		}else if(qName.equals("COD_ELP")){
			currentBuffer = new StringBuffer();
		}else if(qName.equals("LIB_ELP")){
			currentBuffer = new StringBuffer();
		}else if(qName.equals("G_ETU")){
			currentEtudiant = new Etudiant();
		}else if(qName.equals("LIB_NOM_PAT_IND")){
			currentBuffer = new StringBuffer();
		}else if(qName.equals("LIB_PR1_IND")){
			currentBuffer = new StringBuffer();
		}else if(qName.equals("COD_ETU")){
			currentBuffer = new StringBuffer();
		}else if(qName.equals("LIST_G_ELP")){
			try {
				db.beginTransaction();
			} catch (SQLException e) {
				new SAXException(e);
			}
		}
		
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		
		if(qName.equals("G_ELP")){
			try {
				db.ajouterUe(currentUe);
			} catch (SQLException e) {
				throw new SAXException(e);
			}
			for (Etudiant etu : currentEtudiants) {
				try {
					db.ajouterEtudiantSiInexistant(etu);
				} catch (SQLException e) {
					throw new SAXException(e);
				}
				try {
					db.ajouterEtudiantAUe(currentUe, etu);
				} catch (SQLException e) {
					throw new SAXException(e);
				}
			}
			currentUe = null;
			currentEtudiants = null;
		}else if(qName.equals("COD_ELP")){
			currentUe.setCode(currentBuffer.toString());
			currentBuffer = null;
		}else if(qName.equals("LIB_ELP")){
			currentUe.setLibelle(currentBuffer.toString());
			currentBuffer = null;
		}else if(qName.equals("G_ETU")){
			currentEtudiants.add(currentEtudiant);
		}else if(qName.equals("LIB_NOM_PAT_IND")){
			currentEtudiant.setNom(currentBuffer.toString());
			currentBuffer = null;
		}else if(qName.equals("LIB_PR1_IND")){
			currentEtudiant.setPrenom(currentBuffer.toString());
			currentBuffer = null;
		}else if(qName.equals("COD_ETU")){
			currentEtudiant.setNumero(Integer.parseInt(currentBuffer.toString()));
			currentBuffer = null;
		}else if(qName.equals("LIST_G_ELP")){
			try {
				db.endTransaction();
			} catch (SQLException e) {
				new SAXException(e);
			}
		}
	}
	
}
